<?php
include 'infobd.php';
include 'accent.php';

$action = $_REQUEST['action'];
$nb = $_REQUEST['nb'];
$cours = $_REQUEST['cours'];
$date = $_REQUEST['date'];
$description = $_REQUEST['description'];
$requete = "";

if (!$action){
echo 'Erreur de transfert';
}
else{
	if($action) == "select"){
		select();
	}
	elseif($action) == "insert"){
		if (!$nb || !$cours || !$date || !$description){echo 'Erreur de transfert';}
		else{
			$description = str_replace($normalaccent, $codeaccent, $description);
			$requete = sprintf("insert into devoir (nb, cours, date, description) values('%s','%s', '%s', %s)",
    		mysql_real_escape_string($nb),
    		mysql_real_escape_string($cours),
			mysql_real_escape_string($date),
			mysql_real_escape_string($description));
		}
	}
	elseif($action == "delete")
		if(!nb){echo 'Erreur de transfert';}
		else{
			$requete = sprintf("delete from devoir where nb = '%s'",
    		mysql_real_escape_string($nb));
		}
	}
	elseif($action == "update"){
		if (!$nb || !$cours || !$date || !$description){echo 'Erreur de transfert';}
		else{
			$description = str_replace($normalaccent, $codeaccent, $description);
			$requete = sprintf("update devoir set cours = '%s', date = '%s', description = '%s' where nb = '%s'",
    		mysql_real_escape_string($cours),
			mysql_real_escape_string($date),
			mysql_real_escape_string($description),
			mysql_real_escape_string($nb));
		}
	}
	if ($requete != ""){
		$con = mysql_connect($serv,$user,$passw);
		if (!$con)
		{
			die('Could not connect: ' . mysql_error());
		}
	 
		mysql_select_db($db, $con);
		if (!mysql_query($requete,$con))
		{
			die('Error: ' . mysql_error());
		}
	}
	mysql_close($con);
	select();
}

function select(){
	include 'database.php';
	$srequete = "select nb, cours, date, description from devoir order by date";
	$con = mysql_connect($serv,$user,$passw);
	if (!$con)
	{
		die('Could not connect: ' . mysql_error());
	}
	mysql_select_db($db, $con);
	$result = mysql_query($srequete);
	$index = 1;
	echo "<table border='0' id='tb' border='1' width='900'>";
	echo "<thead><tr><th width="100">Cours</th><th width="100">Date</th><th width="300">Description</th><th width="200">Action</th></tr></thead><tbody>";
	while($row = mysql_fetch_array($result))
	{
		echo "<tr><td>" . $row['cours'] . "</td><td>" . $row['date'] . "</td><td>" . $row['description'] . "</td><td>";
		echo "<input type='button' id='update' value='Update' onclick=\"updatedev('" . $index . "', [" . $row['nb'] . $row['cours'] . "," . $row['date'] . "," . $row['description'] . "]);\"/>";
		echo "<input type='button' id='delete' value='Delete' onclick=\"deletedev('" . $row['nb'] . "');\"/></td></tr>";
		$index++;
	}
	echo "<tr><td><input type='text' id='cours' value=''/></td><td><input type='text' id='date' value=''/></td><td><input type='text' id='description' value=''/></td><td><input type='button' value='Insert' onclick='insertdev();'/></td></tr>";
	echo "</tbody></table>";
}
?>